<?php
/* 
 * Run crontab in PHP
 * @Author: hooligan - contact@vblog-it.com
 */
?>
<form action="" method="post" name="frm-crontab">
    <h1>Crontab for weekly</h1>
    Day of week
    <select name="day_of_week">
        <option value="0">SUN</option>
        <option value="1">MON</option>
        <option value="2">TUE</option>
        <option value="3">WED</option>
        <option value="4">THU</option>
        <option value="5">FRI</option>
        <option value="6">SAT</option>
    </select>
    Hour
    <select name="hour">
        <?php for ($i = 0; $i < 24; $i++): ?>
        <option value="<?php echo $i < 10 ? '0'. $i: $i; ?>"><?php echo $i ?></option>
        <?php endfor; ?>
    </select>
    Minute
    <select name="minute">
        <?php for ($i = 0; $i < 60; $i++): ?>
        <option value="<?php echo $i < 10 ? '0'. $i: $i; ?>"><?php echo $i ?></option>
        <?php endfor; ?>
    </select>
    <button name="btn-run-crontab" type="submit">Run crontab</button>
</form>

<?php 
    if (isset($_POST['btn-run-crontab'])) {
        $day_of_week = $_POST['day_of_week'];
        $hour = $_POST['hour'];
        $minute = $_POST['minute'];
        $cmd =  intval($minute). ' '. intval($hour). ' * * '. $day_of_week;
        $cmd .= ' wget http://php.dev/crontab/crontest.php'. PHP_EOL; // Phai co PHP_EOL
        
        // Put the command to the crontab.txt
        $filename = 'crontab.txt';
        if (!file_exists($filename)) {
            $fp = fopen($filename, 'wp');
            @fclose($fp);
        }
        
        file_put_contents($filename, $cmd);
        
        // Remove crontab
        exec('crontab -r');
        
        // Excute crontab.txt
        echo exec('crontab '. $filename);
    }
?>



